![]() |
Java Database Programming with JDBC
by Pratik Patel Coriolis, The Coriolis Group ISBN: 1576100561 Pub Date: 10/01/96 |
Previous | Table of Contents | Next |
There are two types of events that we need to be aware of in the IconStore application: selecting menu options and clicking on the image list to select an icon. As with the Interactive SQL applet we discussed in Chapter 4, the event handling code is contained in the handleEvent method, as shown in Listing 8.8.
Listing 8.8 IconStore handleEvent.
// // handleEvent // Handle an event by the user. // public boolean handleEvent( Event evt) { switch (evt.id) { case Event.ACTION_EVENT: // Determine the type of event that just occurred if (evt.target instanceof MenuItem) { // The user selected a menu item. Figure out what action // should be taken. String selection = (String) evt.arg; // 'Save As' - Save the currently displayed icon to a file if (selection.equals("Save As")) { if (currentFile != null) { fileDialog.setFile(""); fileDialog.pack(); fileDialog.show(); String saveFile = fileDialog.getFile(); if (saveFile == null) { return true; } // If this is a new file, it will end with .*.* if (saveFile.endsWith(".*.*")) { saveFile = saveFile.substring(0, saveFile.length() - 4); // If no extension is given, append .GIF if (saveFile.indexOf(".") < 0) { saveFile += ".gif"; } } // Copy the file. Returns true if successful. boolean rc = copyFile (currentFile, saveFile); } return true; } // 'Exit' - Exit the application else if (selection.equals("Exit")) { // If there was an image file, delete it if (currentFile != null) { (new File(currentFile)).delete(); } System.exit(0); } // The user must have selected a different set of icons; // Display the proper list. else { currentList = selection; ((CardLayout) iconListPanel.getLayout()).show( iconListPanel, currentList); // Display the icon, if one was previously selected displayIcon(connection); return true; } } break; case Event.LIST_SELECT: displayIcon(connection); break; } return false; }
Most of the code is very straightforward. Of interest here is how the CardLayout is managed. When a user makes a selection from the Icons menu, the selected item (which is the category description) is used to change the CardLayout. Remember that when the CardLayout was created, the title of each list was the category description. Also note that when the user selects an item from the list box (LIST_SELECT), the corresponding image can be displayed. Listing 8.9 shows how this is done.
When the user selects Exit from the menu, the temporary image file (which is discussed later) is deleted from disk, and the application is terminated. This is the perfect time to close the Connection that was in use. I purposefully omitted this step to illustrate a point: The JDBC specification states that all close operations are purely optional. It is up to the JDBC driver to perform any necessary clean-up in the finalize methods for each object. I strongly recommend, though, that all JDBC applications close objects when it is proper to do so.
Listing 8.9 Loading and displaying the selected image.
// // displayIcon // Display the currently selected icon. // public void displayIcon( Connection con) { // Get the proper list element int n = getCategoryElement(currentList); // Get the item selected String item = lists[n].getSelectedItem(); // Only continue if an item was selected if (item == null) { return; } // Get the ID String id = (String) iconDesc[n].get(item); try { // Create a Statement object Statement stmt = con.createStatement(); // Execute the query and process the results ResultSet rs = stmt.executeQuery( "SELECT ICON FROM ICONSTORE WHERE ID=" + id); // If no rows are returned, the icon was not found if (!rs.next()) { stmt.close(); return; } // Get the data as an InputStream InputStream inputStream = rs.getBinaryStream(1); if (inputStream == null) { stmt.close(); return; } // Here's where things get ugly. Currently, there is no way // to display an image from an InputStream. We'll create a // new file from the InputStream and load the Image from the // newly created file. We need to create a unique name for // each icon; the Java VM caches the image file. String name = myHome + "/IconStoreImageFile" + id + ".gif"; FileOutputStream outputStream = new FileOutputStream(name); // Write the data int bytes = 0; byte b[] = new byte[1024]; while (true) { // Read from the input. The number of bytes read is returned. bytes = inputStream.read(b); if (bytes == -1) { break; } // Write the data outputStream.write(b, 0, bytes); } outputStream.close(); inputStream.close(); // Close the statement stmt.close(); // Now, display the icon loadFile(name); // If there was an image file, delete it if (currentFile != null) { if (!currentFile.equals(name)) { (new File(currentFile)).delete(); } } // Save our current file name currentFile = name; } catch (SQLException ex) { // An SQLException was generated. Dump the exception contents. // Note that there may be multiple SQLExceptions chained // together. System.out.println("\n*** SQLException caught ***\n"); while (ex != null) { System.out.println("SQLState: " + ex.getSQLState()); System.out.println("Message: " + ex.getMessage()); System.out.println("Vendor: " + ex.getErrorCode()); ex = ex.getNextException(); } System.exit(1); } catch (java.lang.Exception ex) { ex.printStackTrace(); System.exit(1); } }
Previous | Table of Contents | Next |